﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Web.Utility;
using App.Domain;
using App.Core.Extensions;

namespace App.Web.Controllers
{
    public class HomeController : BaseController
    {
        private AppEntities _Context = new AppEntities();

        public ActionResult Index()
        {
            //int pageSize = ConfigReader.pageSize;
            //int pageNo = 1;
            //UtilityDAO dao = new UtilityDAO();
            //DbParameter[] parameters = new[] { 
            //            new DbParameter("@CompanyID", 1),
            //            new DbParameter("@UserID",(SessionCache.CurrentUser==null)? 0 : SessionCache.CurrentUser.ID)
            //        };
            //int totalRecord = 0;
            //System.Data.DataSet ds =
            //    dao.GetPagedData(DbObject.SP.GET_PRODUCT_RANDOMLY,
            //    parameters, pageNo, 10, out totalRecord, true);

            //ViewBag.PageNumber = pageNo;
            //ViewBag.PageSize = pageSize;
            //ViewBag.TotalRecord = totalRecord;
            //ViewBag.DataSource = ds;

            //var query = from P in _Context.Companies.Include("District.Country") where P.IsActive orderby P.Name select P;
            //ViewData[AppConstants.ViewDataKeys.COMPANY_LIST] = query == null ? new List<Company>() : query.ToList();

            return View();
        }

        #region Obsolete Code

        /// <summary>
        /// Shows the content details page.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //public ActionResult Index(String id)
        //{
        //    return View("ContentDetails");
        //}
        
        //public ActionResult Index(String page = null, String SearchKey = "")
        //{
        //    //ViewBag.Message = "Welcome to ASP.NET MVC!";
        //    //int pageNo = id.ToInt() > 0 ? id.ToInt() : 1;
        //    int pageSize = ConfigReader.pageSize;


        //    if (SearchKey.ToString().Trim() == "")
        //    {
        //        int pageNo = page.ToInt() > 0 ? page.ToInt() : 1;
        //        UtilityDAO dao = new UtilityDAO();
        //        DbParameter[] parameters = new[] { 
        //            new DbParameter("@CompanyID", 1),
        //            new DbParameter("@UserID",(SessionCache.CurrentUser==null)? 0 : SessionCache.CurrentUser.ID)
        //        };
        //        int totalRecord = 0;
        //        System.Data.DataSet ds = 
        //            dao.GetPagedData(DbObject.SP.GET_PRODUCT_RANDOMLY,
        //            parameters, pageNo, 10, out totalRecord, true);

        //        ViewBag.PageNumber = pageNo;
        //        ViewBag.PageSize = pageSize;
        //        ViewBag.TotalRecord = totalRecord;
        //        ViewBag.DataSource = ds;
        //        //ViewBag.ShowEditLink = true;

        //        //ViewModels.UtilityModels.PagerConfig pagerConfig = new ViewModels.UtilityModels.PagerConfig();
        //        //pagerConfig.PageNo = pageNo;
        //        //pagerConfig.PageSize = pageSize;
        //        ////pagerConfig.TotalRecordMessage = "Total {0} Products Found";


        //        //pagerConfig.UrlFormat = "/Home/Index?id={0}";

        //        //ViewBag.PagerConfig = pagerConfig;
        //        //ViewBag.TotalRecord = pagerConfig.TotalRecord = totalRecord;

        //        return View();
        //    }

        //    else if (SearchKey.ToString().Trim() == "Pupular Search")
        //    {
        //        int pageNo = page.ToInt() > 0 ? page.ToInt() : 1;
        //        UtilityDAO dao = new UtilityDAO();
        //        DbParameter[] parameters = new[] { 
        //        new DbParameter("@CompanyID", 1), 
        //        //new DbParameter("@SearchKey", SearchKey) 
        //        };
        //        int totalRecord = 0;
        //        System.Data.DataSet ds = dao.GetPagedData(DbObject.SP.GET_PRODUCT_POPULAR_SEARCH, parameters, pageNo, 10, out totalRecord, true);
        //        ViewBag.PageNumber = pageNo;
        //        ViewBag.PageSize = 10;
        //        ViewBag.TotalRecord = totalRecord;
        //        ViewBag.DataSource = ds;
        //        //ViewBag.ShowEditLink = true;

        //        return View();
        //    }

        //    //else if (SearchKey.ToString().Trim() == "Latest Added")
        //    //{
        //    //    int pageNo = page.ToInt() > 0 ? page.ToInt() : 1;
        //    //    UtilityDAO dao = new UtilityDAO();
        //    //    DbParameter[] parameters = new[] { 
        //    //    new DbParameter("@CompanyID", 1), 
        //    //    //new DbParameter("@SearchKey", SearchKey) 
        //    //    };
        //    //    int totalRecord = 0;
        //    //    System.Data.DataSet ds = dao.GetPagedData(DbObject.SP.GET_PRODUCT_BY_LATEST_ADDED, parameters, pageNo, 10, out totalRecord, true);
        //    //    ViewBag.PageNumber = pageNo;
        //    //    ViewBag.PageSize = 10;
        //    //    ViewBag.TotalRecord = totalRecord;
        //    //    ViewBag.DataSource = ds;
        //    //    //ViewBag.ShowEditLink = true;

        //    //    return View();
        //    //}

        //    else
        //    {
        //        int pageNo = page.ToInt() > 0 ? page.ToInt() : 1;
        //        UtilityDAO dao = new UtilityDAO();
        //        DbParameter[] parameters = new[] { 
        //        new DbParameter("@CompanyID", 1), 
        //        new DbParameter("@SearchKey", SearchKey) 
        //        };
        //        int totalRecord = 0;
        //        System.Data.DataSet ds = dao.GetPagedData(DbObject.SP.GET_PRODUCT_BY_SEARCH, parameters, pageNo, 10, out totalRecord, true);
        //        ViewBag.PageNumber = pageNo;
        //        ViewBag.PageSize = 10;
        //        ViewBag.TotalRecord = totalRecord;
        //        ViewBag.DataSource = ds;
        //        //ViewBag.ShowEditLink = true;

        //        return View();
        //    }
        //}

        ////public ActionResult Index(String SearchKey = "", String page = null)
        ////{
        ////    //ViewBag.Message = "Welcome to ASP.NET MVC!";

        ////    int pageNo = page.ToInt() > 0 ? page.ToInt() : 1;
        ////    UtilityDAO dao = new UtilityDAO();
        ////    DbParameter[] parameters = new[] { 
        ////        new DbParameter("@CompanyID", 1), 
        ////        new DbParameter("@SearchKey", SearchKey) 
        ////    };
        ////    int totalRecord = 0;
        ////    System.Data.DataSet ds = dao.GetPagedData(DbObject.SP.GET_PRODUCT_BY_SEARCH, parameters, pageNo, 10, out totalRecord, true);
        ////    ViewBag.PageNumber = pageNo;
        ////    ViewBag.PageSize = 10;
        ////    ViewBag.TotalRecord = totalRecord;
        ////    ViewBag.DataSource = ds;
        ////    //ViewBag.ShowEditLink = true;

        ////    return View();
        ////}
        #endregion Obsolete Code

        public ActionResult About()
        {
            return View();
        }

        /// <summary>
        /// Gets District Collection By a Country
        /// </summary>
        /// <param name="countryID"></param>
        /// <returns></returns>
        public String GetDistrictsByCountry(int countryID)
        {
            var query = from P in _Context.Districts
                        where P.CountryID == countryID
                        orderby P.Name
                        select new
                        {
                            ID = P.ID,
                            Name = P.Name
                        };
            if (query == null || query.Count() == 0)
                return null;
            else
                return query.ToList().ToJSON();
        }
    }
}
